package com.yuwei.service;

import com.yuwei.bean.Meta;
import com.yuwei.bean.User;
import com.yuwei.bean.Users;
import com.yuwei.bean.UsersData;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserService {
    //    key=id,value=user
    private Map<Integer, User> userMap = new HashMap<>();
    private int id = 400;

    private User createUser(String userName, String pwd) {
        User user = new User();
        user.setUserName(userName);
        user.setUserPwd(pwd);
        user.setId(id);
        user.setRole_name("测试");
        user.setMs_state(false);
        user.setMobile("12345678999");
        id++;
        return user;
    }

    public String getUserPwdByName(String userName) {
        for (User user : userMap.values()) {
            String userNameTmp = user.getUserName();
            if (userNameTmp.equals(userName)) {
                return user.getUserPwd();
            }
        }
        return null;
    }

    public List<User> getUserByName(String userName) {
        List<User> search = new ArrayList<>();
        for (User user : userMap.values()) {
            String userNameTmp = user.getUserName();
            if (userNameTmp.contains(userName)) {
                search.add(user);
            }
        }
        return search;
    }

    public User findUserByName(String userName) {
        for (User user : userMap.values()) {
            String userNameTmp = user.getUserName();
            if (userNameTmp.contains(userName)) {
                return user;
            }
        }
        return null;
    }

    /**
     * @param query    查询的用户
     * @param pagenum  当前的页数
     * @param pagesize 当前每页显示多少条
     * @return
     */
    public Users getUsers(String query, int pagenum, int pagesize) {
        Users users = new Users();

        Meta meta = createMeta();
        users.setMeta(meta);

        UsersData usersData = new UsersData();
        users.setData(usersData);

        if ("".equals(query)) {
            int usersCount = userMap.size();
            usersData.setTotal(usersCount);
            List<User> userList = new ArrayList<>(userMap.values());
            int startPos = (pagenum - 1) * pagesize;
            int endPos = pagenum * pagesize;
            if (pagenum * pagesize > usersCount) {
                endPos = usersCount;
            }
            List<User> userListPage = userList.subList(startPos, endPos);
            usersData.setUsers(userListPage);
        } else {
            List<User> search = getUserByName(query);
            usersData.setUsers(search);
            usersData.setPagenum(1);
            usersData.setTotal(search.size());
        }
        return users;
    }

    public User addUser(User user) {
        String userName = user.getUserName();
        User userTmp = findUserByName(userName);
        if (userTmp != null) {
            return null;
        }
        User newUser = createUser(userName, user.getUserPwd());
        userMap.put(newUser.getId(), newUser);
        return newUser;
    }

    public User update(int id, boolean state) {
        boolean isExist = userMap.containsKey(id);
        if (isExist) {
            User user = userMap.get(id);
            user.setMs_state(state);
            System.out.println("更新用户成功.Id:" + id + ",状态:" + state);
            return user;
        }
        User user = new User();
        user.setId(id);
        user.setMs_state(state);
        userMap.put(id, user);
        System.out.println("没有查找到用户，添加新用户.Id:" + id + ",状态:" + state);
        return user;
    }

    public User update(int id, User user) {
        boolean isExist = userMap.containsKey(id);
        if (isExist && user != null) {
            String email = user.getEmail();
            String mobile = user.getMobile();
            User user1 = userMap.get(id);
            user1.setMobile(mobile);
            user1.setEmail(email);
            return user1;
        }
        return null;
    }

    public boolean delUserByID(int id) {
        userMap.remove(id);
        return true;
    }

    public User getUserByID(int id) {
        boolean isExist = userMap.containsKey(id);
        if (!isExist) {
            return null;
        }
        return userMap.get(id);
    }

    @PostConstruct
    void init() {
        User admin = createUser("admin", "123456");
        admin.setRole_name("超级管理员");
        admin.setMobile("17396716569");
        admin.setEmail("88888888@qq.com");
        userMap.put(admin.getId(), admin);

        User testRole2 = createUser("特朗普", "1234567");
        testRole2.setRole_name("搞笑的");
        testRole2.setMobile("17396716569");
        testRole2.setEmail("1111@qq.com");
        testRole2.setMs_state(false);
        userMap.put(testRole2.getId(), testRole2);

        User zoufeng = createUser("猪八戒", "1234567");
        zoufeng.setRole_name("颜值担当");
        zoufeng.setMobile("17396716569");
        zoufeng.setEmail("1111@qq.com");
        zoufeng.setMs_state(false);
        userMap.put(zoufeng.getId(), zoufeng);

        User lyf = createUser("刘亦菲", "1234567");
        lyf.setRole_name("明星");
        lyf.setMobile("17396716569");
        lyf.setEmail("2221@qq.com");
        lyf.setMs_state(true);
        userMap.put(lyf.getId(), lyf);

        User lss = createUser("刘诗诗", "1234567");
        lss.setRole_name("明星");
        lss.setMobile("17396716569");
        lss.setEmail("1144441@qq.com");
        lss.setMs_state(false);
        userMap.put(lss.getId(), lss);

    }

    private Meta createMeta() {
        Meta meta = new Meta();
        meta.setMsg("获取管理员列表成功");
        meta.setStatus(200);
        return meta;
    }
}
